package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingServiceImpl implements ShoppingService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingMapper shoppingMapper;



    /**
     * 购物车添加商品
     * @param shoppingCartDTO
     */
    @Transactional
    @Override
    public void addShopping(ShoppingCartDTO shoppingCartDTO) {

        // 查询购物车中是否有该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        // 获取登陆用户的ID
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        // 动态获取购物车数据
        List<ShoppingCart> shoppingCarts = shoppingMapper.listShopping(shoppingCart);

        // 判断是否有该信息
        if(shoppingCarts != null && shoppingCarts.size() > 0){
            // 获取查询到的一条数据
            ShoppingCart cart = shoppingCarts.get(0);
            cart.setNumber(cart.getNumber()+1);
            // 修改数据
            shoppingMapper.updateNumberById(cart);
        }else{
            // 不存在就插入一条数据
            if(shoppingCartDTO.getDishId() != null){
                // 添加的时菜品数据
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
//                shoppingCart.setNumber(1);
//                shoppingCart.setCreateTime(LocalDateTime.now());
            }else{
                Setmeal setmeal = setmealMapper.selectSetmeal(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 购物车中添加数据
            shoppingMapper.insertShopping(shoppingCart);
        }
    }


    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> selectShopping() {
        // 获取当前的用户ID
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> shoppingCartList = shoppingMapper.selectShoppingByUserId(currentId);
        //List<ShoppingCart> shoppingCartList = shoppingMapper.listShopping(shoppingCart);
        return shoppingCartList;
    }


    /**
     * 清空购物车
     */
    @Override
    public void deleteShopping() {
        // 获取当前的用户ID
        Long currentId = BaseContext.getCurrentId();
        shoppingMapper.deleteShoppingByUserId(currentId);
    }


    /**
     * 删除购物车中的一条物品
     */
    @Override
    public void deleteOneShopping(ShoppingCartDTO shoppingCartDTO) {
        // 查询购物车中是否有该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        // 获取登陆用户的ID
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        // 动态获取购物车数据
        List<ShoppingCart> shoppingCarts = shoppingMapper.listShopping(shoppingCart);

        // 判断是否有该信息
        if(shoppingCarts != null && shoppingCarts.size() > 0) {
            // 获取查询到的一条数据
            ShoppingCart cart = shoppingCarts.get(0);
            cart.setNumber(cart.getNumber() - 1);
            // 修改数据
            if(cart.getNumber() == 0){
                shoppingMapper.deleteShoppingByUserId(currentId);
            }
            shoppingMapper.updateNumberById(cart);
        }
    }
}
